<?php
/**
 * DotHerbert
 *
 * @package    loader
 * @copyright  2009 extreme-programmers
 * @license    http://dotherbert.extreme-programmers.com/license   BSD License
 * @version    $Id:$
 */

namespace dotherbert\loader;
use dotherbert\core\stub;

/**
 * Resource autoloader
 *
 * @package    loader
 * @author     Johannes Trink <johannes.trink@extreme-programmers.com>
 */
class ResourceAutoloader implements AutoloaderInterface
{
    /**
     * Namespace
     *
     * @var string
     */
    private $namespace;

    /**
     * Constructor
     *
     * @param string $namespace
     */
    public function __construct($namespace)
    {
        $this->namespace = $namespace;
    }

    /**
     * Get namespace
     *
     * @return string
     */
    public function getNamespace()
    {
        return $this->namespace;
    }

    /**
     * Autoload
     *
     * @param string $class
     * @return bool
     */
    public function autoload($class)
    {
        if (0 !== strpos($class, $this->namespace)) {
            return false;
        }

        $segment = substr($class, strlen($this->namespace) + 1);

        $file = str_replace('\\', '/', $segment) . '.php';
        return stub\ControlStructureStub::getInstance()->includeFile($file);
    }
}